import animation
import cameraUtils
import GameWorld
import math
import mathCommon
import geo2
import svc
import log
MAXIMUM_HEAD_LOOK_ANGLE = 2.5
KEY_TURN_ANGLE = 0.5

class PlayerAnimationController(animation.BipedAnimationController):
    __guid__ = 'animation.PlayerAnimationController'

    def Run(self):
        self.entityRef = None
        self.walkSpeed = 2.0
        self.flyMode = False
        self.currentLOD = 0
        self.cameraBoundRotation = True
        self.cameraClient = sm.GetService('cameraClient')
        self.aimingClient = sm.GetService('aimingClient')
        self.entityService = sm.GetService('entityClient')
        self.mouseInputService = sm.GetService('mouseInput')
        self.mouseInputService.RegisterCallback(const.INPUT_TYPE_MOUSEDOWN, self.OnMouseDown)
        self.mouseInputService.RegisterCallback(const.INPUT_TYPE_MOUSEUP, self.OnMouseUp)



    def Stop(self, stream):
        self.mouseInputService.UnRegisterCallback(const.INPUT_TYPE_MOUSEDOWN, self.OnMouseDown)
        self.mouseInputService.UnRegisterCallback(const.INPUT_TYPE_MOUSEUP, self.OnMouseUp)



    def OnMouseDown(self, button, posX, posY, entityID):
        if (button is const.INPUT_TYPE_RIGHTCLICK):
            self.cameraBoundRotation = False



    def OnMouseUp(self, button, posX, posY, entityID):
        if (button is const.INPUT_TYPE_RIGHTCLICK):
            self.cameraBoundRotation = True



    def SetFlyMode(self, flyMode):
        self.flyMode = flyMode



    def _UpdateHook(self):
        animation.BipedAnimationController._UpdateHook(self)



    def UpdateLookAtTarget(self):
        animation.BipedAnimationController.UpdateLookAtTarget(self)



    def UpdateMovement(self):
        self.ProcessNewMovement(self.entityRef.movement.avatar.localHeading)



    def UpdateIdlePose(self):
        return 



    def ProcessNewMovement(self, heading):
        headingLength = geo2.Vec3Length(heading)
        self.SetControlParameter('Speed', headingLength)
        isPathing = isinstance(self.entityRef.movement.avatar.GetActiveMoveMode(), GameWorld.PathToMode)
        if not isPathing:
            if (headingLength > const.FLOAT_TOLERANCE):
                if self.cameraBoundRotation:
                    turnAngle = cameraUtils.GetAngleFromEntityToYaw(self.entityRef, cameraUtils.CalcDesiredPlayerHeading(heading))
                    turnAngle = (turnAngle / math.pi)
                else:
                    turnAngle = 0.0
                    if (heading[0] > 0.0):
                        turnAngle = -KEY_TURN_ANGLE
                    elif (heading[0] < 0.0):
                        turnAngle = KEY_TURN_ANGLE
                absTurnAngle = abs(turnAngle)
                if (absTurnAngle < const.FLOAT_TOLERANCE):
                    turnAngle = 0.0
                self.SetControlParameter('TurnAngle', turnAngle)
            else:
                self.SetControlParameter('TurnAngle', 0)




